<?php

namespace IDock\Insurance\QHIC\Response;

use IDock\Http\Response;
use IDock\Insurance\QHIC\Response\AllQHICResponse;
use IDock\Printer\PolicyPaymentPrinter;

class PolicyPaymentResponse extends AllQHICResponse
{
    /**
     * @var PolicyPaymentPrinter $printer
     */
    public $printer = PolicyPaymentPrinter::class;

    public function printerInit(): Response
    {
        $body = $this->getBody();

        if ($this->printer) {
            $this->printer = new $this->printer;
        }
        $status = $body['CStatus'] ?? '';
        $msg = $body['Message'] ?? '';
        if($status == '0'){
            $this->setError($msg);
            return $this;
        }
        $this->printer->payUrl = $body['CSignUrl'] ?? '';
        if(empty($this->printer->payUrl)){
            throw new \Exception("获取支付链接失败");
        }
        return $this;
    }
}
